package com.gxa.finance.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.common.commons.R;
import com.gxa.finance.controller.*;
import com.gxa.finance.pojo.Bill;
import com.gxa.finance.pojo.Flow;
import com.gxa.finance.service.PayService;
import com.gxa.order.feign.ServiceOrderApi;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Map;

@Service
public class PayServiceImpl implements PayService {

    @Resource
    BillController billController;
    @Resource
    CollectionAccountController collectionAccountController;
    @Resource
    FlowController flowController;
    @Resource
    UserAccountController userAccountController;
    @Resource
    OfficeTransferReviewController officeTransferReviewController;
    @Resource
    ServiceOrderApi serviceOrderApi;

    /**
     * 酒店订单支付
     * @param bill 收款计划实例
     * @return R
     */
    @Override
    public R tavernPay(Bill bill) {
        //用户账户扣款
        userAccountController.updateUserAccountMoney(bill.getAccount(),bill.getMoney());
        //收款账户添款
        collectionAccountController.updateAccountMoney(bill.getMoney());
        //修改财务模块订单状态、支付时间
        bill.setPaymentTime(new Timestamp(System.currentTimeMillis()));
        bill.setbStatus(2);
        billController.updateBillStatus(bill);
        //添加资金流水
        flowController.addOrUpdateFlow(new Flow(bill.getOrderId(),bill.getpType(),
                bill.getbType(),bill.getMoney(),bill.getPaymentTime(),bill.getPayment()));
        //根据订单编号查询对应的订单id
        int id = (Integer)serviceOrderApi.findid(bill.getOrderId()).getData();
        //修改订单模块支付方式、支付时间、订单状态
        serviceOrderApi.updateHotelOrder(bill.getPaymentTime().toString(),1,2,id);
        //返回数据
        R r = new R();
        r.setCode(200);
        r.setMsg("success");
        return r;
    }



    @Override
    public R servicePay(Bill bill) {
        R r = new R();
        //对用户账户扣去金额
        BigDecimal Money = bill.getMoney();
        String account = bill.getAccount();
        userAccountController.updateUserAccountMoney(account,Money);
        //支付成功修改账单状态 支付时间
        bill.setPaymentTime(new Timestamp(System.currentTimeMillis()));
        bill.setbStatus(2);
        billController.updateBillStatus(bill);
        //向流水管理插入一条数据 付款流水
        Flow flow = new Flow();
        flow.setMoney(bill.getMoney());
        flow.setOrderType(bill.getpType());
        flow.setOrderId(bill.getOrderId());
        flow.setPayTime(bill.getPaymentTime());
        flow.setType(bill.getbType());
        flow.setPayType(bill.getPayment());
        flowController.addOrUpdateFlow(flow);
        //修改服务订单状态

        //向平台存入金额
        collectionAccountController.updateAccountMoney(bill.getMoney());

        r.setCode(200);
        r.setMsg("支付成功");
        return r;
    }



    @Override
    public R serviceOfflinePay(Bill bill) {
        R r = new R();
        try {
            //1、客户账户扣钱
            String account = bill.getAccount();//获取用户账户
            BigDecimal yMoney = bill.getyMoney();//获取已扣除的钱
            userAccountController.updateUserAccountMoney(account, yMoney);//根据账户扣钱

            //2、客户上传凭证,提交审核、审核通过
            bill.setbStatus(2);//修改为已通过
            bill.setPaymentTime(new Timestamp(System.currentTimeMillis()));//设置支付时间
            officeTransferReviewController.updatefindOfficeDetailsReview(bill);

            //3、系统账户收到钱
            BigDecimal getMoney = yMoney; //系统账户收到的钱
            collectionAccountController.updateAccountMoney(getMoney);//存入账户

            //4、添加(修改)资金流水
            Flow flow = new Flow();
            flow.setMoney(bill.getMoney());
            flow.setOrderType(bill.getpType());
            flow.setOrderId(bill.getOrderId());
            flow.setPayTime(bill.getPaymentTime());
            flow.setType(bill.getbType());
            flow.setPayType(bill.getPayment());
            flowController.addOrUpdateFlow(flow);

            r.setCode(200);
            r.setMsg("支付成功");
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员");
        }
        return null;
    }
}
